import { Mut, VueService } from 'vue3-oop'
import UserApi from "../UserApi";
import {Injectable} from "injection-js";
import AllcRoleSercice from "./panel/allcrole.sercice";
import AllcPrmsSercice from "./panel/allcprms.service";
import ValidPrmsSercice from "./panel/validprms.service";

@Injectable()
export default class IndexService extends VueService {

  constructor(
    public allcRoleService: AllcRoleSercice,
    public allcPrmsService: AllcPrmsSercice,
    public validPrmsService: ValidPrmsSercice
  ){
    super();
  }

  @Mut() state = {
    editable: false,
    srcData: {
      id:"", personname:"", tenantId:"", username:""
    },
    editData: {
      id:"", personname:"", tenantId:"", username:""
    }
  }

  /**
   * 切换编辑模式
   */
  toggleEditable = ()=>{
    this.state.editData = {...this.state.srcData};
    this.state.editable = !this.state.editable;
  }

  /**
   * 保存编辑数据
   */
  saveEditData = ()=>{
    UserApi.update({record:this.state.editData}).then(({data})=>{
      if(data.success){
        window.$message.success("更新成功");
        this.loadUserData();
      }
      this.state.editable = false;
    })
  }

  @Mut() userId = "";

  /**
   * 加载用户信息
   */
  loadUserData=()=>{
    const currentRoute = window.$router.currentRoute.value;
    UserApi.get(currentRoute.params.id).then(({data})=>{
      if(data.success && data.record){
        const {id, personname, tenantId, username} = data.record;
        this.state.srcData = {id, personname, tenantId, username};
        this.state.editData = {...this.state.srcData};

        this.allcRoleService.init(id,personname);
        this.allcPrmsService.init(id,personname);
        this.validPrmsService.init(id,personname);
      }
    });
  }

}
